import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    redirect:"/home/one"
  },
  {
    path: "/Login",
    name: "Login",
    component: Login,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  {
    path: '/Home',
    name: 'home',
    component: Home,
    children:[
      {
        path:"one",
        component:()=>import("../components/one.vue")
      },
      {
        path:"two",
        component:()=>import("../components/two.vue")

      },
      {
        path:"three",
        component:()=>import("../components/three.vue")

      }
    ]
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to,from,next)=>{ // 路由守卫不使用循环
  // to去哪里 form上一个
  let token = localStorage.getItem("token")
   console.log(token)
  console.log(to,from)
  if(to.path === "/Login"){
    next()
  }else{
    if(token){
      next()
    }else{
      next({name:"Login"})
    }
  }
  next()
})
export default router
